import java.io.File;
import java.io.FilenameFilter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Pattern;

public class ConsoleFileExplorer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {
			FileExplorer fe;
			FileNameFilter filter;
			Scanner scanner = new Scanner(System.in);
			
			System.out.println("Hello, Luke! I'm you father!");
			Thread.sleep(2000);
			System.out.println("It was a joke, relax!");

			while (true) {

				System.out.println("Let's start. First, enter a file path");
				String path = scanner.nextLine();

				System.out.println("May The Force be with you!");
				File file = new File(path);
				Thread.sleep(2000);

				if (file.exists()) {
					fe = new FileExplorer(path);
					System.out.println();
					System.out.println("List of files");
					fe.getFilesList();

					System.out.println();
					System.out.println("Present directory hierarchy as tree");
					fe.getTree(1, fe.getRootDir());

					System.out.println();
					System.out.println("List file info (use filter)");
					System.out.println("Input regex: ");
					String regex = scanner.nextLine();

					filter = new FileNameFilter(regex);
					ArrayList<File> list = fe.getFilteredFiles(filter);
					if (!list.isEmpty()) {
						for (File f : list) {
							System.out.println(fe.getFileInfo(f));
						}
					} else {
						System.out.println("Sorry, no matches.");
					}
				} else
					System.out.println("Invalid file path");
				System.out.println("Press any key to continue...");
				scanner.nextLine();
			}

		} catch (Exception e) {

		}

	}

}

class FileNameFilter implements FilenameFilter {

	private Pattern pattern;
	private boolean matches = false;

	public FileNameFilter(String str) {
		this.pattern = Pattern.compile(str);
	}

	public void setMatches(boolean mtc) {
		this.matches = mtc;
	}

	@Override
	public boolean accept(File dir, String name) {
		if (this.matches) {
			return this.pattern.matcher(name).matches();
		} else {
			return (pattern.matcher(name).find());
		}
	}

}

class FileExplorer {

	private String path;

	public FileExplorer(String path) {
		super();
		this.path = path;
	}

	public File getRootDir() {
		return new File(this.path);
	}

	public void getFilesList() {
		File dir = new File(this.path);
		if (dir.isDirectory()) {
			File[] files = dir.listFiles();
			for (File file : files) {
				if (file.isDirectory())
					System.out.println("Directory: " + file.getName());
				else
					System.out.println("File: " + file.getName());
			}
		}

	}

	public void getTree(int indent, File file) {
		for (int i = 0; i < indent; i++)
			System.out.print(" ");
		System.out.println(file.getName());
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (File f : files)
				getTree(indent + 4, f);
		}

	}

	public ArrayList<File> getFilteredFiles(FilenameFilter filter) {
		ArrayList<File> list = new ArrayList<File>();
		Collections.addAll(list, new File(this.path).listFiles(filter));
		return list;
	}

	public String getFileInfo(File file) {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-yyyy hh:mm");
		str =  "Name: " + file.getName() + "\n";
		str += "Path: " + file.getAbsolutePath() + "\n";
		str += "Last modified: " + sdf.format(new Date(file.lastModified())) + "\n";
		return str;
	}

}